package com.weetop.cfw.home_page.activity

import android.os.Bundle
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.ToastUtils
import com.weetop.cfw.R
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.bean.AdTopPriceBean
import com.weetop.cfw.bean.OrderInfoBean
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.utils.MMKVUtils
import com.weetop.cfw.utils.PayUtils
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils
import com.xgr.easypay.callback.IPayCallback
import kotlinx.android.synthetic.main.activity_ad_pay_to_top.*

class AdPayToTopActivity : CommonBaseActivity() {
    override fun getLayoutId() = R.layout.activity_ad_pay_to_top

    var infoId: Int = 0

    override fun initView(savedInstanceState: Bundle?) {
        infoId = intent.getIntExtra("infoId", 0)
        System.out.println("infoId:"+infoId)
        imageBack.setOnClickListener {
            finish()
        }
        getPrice()
    }

    /**
     *  获取指置顶价格
     */
    private fun getPrice() {
        RxJavaUtils.useInAppCompatActivityOnDestory(
            RetrofitUtils.getApi().getAdTopPrice(), this, object :
                RxJavaCallBack<AdTopPriceBean>() {
                override fun onSuccess(t: AdTopPriceBean?) {
                    if (t != null) {
                        tvPrice.text = "¥" + String.format("%.2f", (t.price.toFloat()))
                    }

                }

                override fun onErrorResponse(throwable: Throwable?) {
                    super.onErrorResponse(throwable)
                }

                override fun onRequestError(t: AdTopPriceBean?) {
                    super.onRequestError(t)
                }

            })
    }

    override fun initData(savedInstanceState: Bundle?) {
        stSure.setOnClickListener {
            if (etNum.text.toString() != "") {
                if (StringUtils.isEmpty(etNum.text.toString()) || etNum.text.toString().toInt() == 0) {
                    ToastUtils.showShort("月份选择不得为0")
                } else if (etNum.text.toString().toInt() > 24) {
                    ToastUtils.showShort("月份选择不得超过24个月")
                } else {
                    submitTopOrder()
                }
            } else {
                ToastUtils.showShort("请选择购买月份")
            }
        }
    }

    private fun submitTopOrder() {
        System.out.println("infoId1:"+MMKVUtils.getToken())
        System.out.println("infoId1:"+infoId)
        System.out.println("infoId1:"+etNum.text.toString().toInt())
        System.out.println("infoId1:"+etRemark.text.toString())
        RxJavaUtils.useInAppCompatActivityOnDestory(
            RetrofitUtils.getApi().getAdTopOrder(
                MMKVUtils.getToken(),
                "25",
                infoId.toString(),
                etNum.text.toString().toInt(),
                etRemark.text.toString()
            ), this, object :
                RxJavaCallBack<OrderInfoBean>() {
                override fun onSuccess(t: OrderInfoBean?) {
                    if (t != null) {
                        PayUtils.aliPay(this@AdPayToTopActivity, t.sign, object : IPayCallback {
                            override fun failed(code: Int, message: String?) {
                                ToastUtils.showShort("支付失败")
                            }

                            override fun cancel() {
                                ToastUtils.showShort("支付取消")
                            }

                            override fun success() {
                                ToastUtils.showShort("支付成功")

                            }

                        })
                    }
                }

                override fun onErrorResponse(throwable: Throwable?) {
                    super.onErrorResponse(throwable)
                }

                override fun onRequestError(t: OrderInfoBean?) {
                    super.onRequestError(t)
                }

            })
    }


}
